package com.wangdao.wechatmall.controller.admin;

import com.wangdao.wechatmall.bean.bo.admin.BaseIssueBO;
import com.wangdao.wechatmall.bean.bo.admin.IssueBO;
import com.wangdao.wechatmall.bean.bo.admin.IssueParamBO;
import com.wangdao.wechatmall.bean.pojo.Issue;
import com.wangdao.wechatmall.bean.vo.admin.BaseRespVo;
import com.wangdao.wechatmall.bean.vo.admin.IssueVO;
import com.wangdao.wechatmall.exception.AdminVarcharException;
import com.wangdao.wechatmall.service.admin.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Issue Controller
 * created by feng on 7/8/21
 */

@RestController
@RequestMapping("admin/issue")
public class IssueController {

    @Autowired
    IssueService issueService;

    //http://182.92.235.201:8083/admin/issue/list?page=1&limit=20&sort=add_time&order=desc
    @GetMapping("list")
    public BaseRespVo issue(IssueBO issueBO) throws AdminVarcharException {

        // 如果issueBO 中的长度超过255就会报错
        if (issueBO.getQuestion() != null && issueBO.getQuestion().length() > 255) {
            throw new AdminVarcharException("您输入的字符过长");
        }


        IssueVO issue = issueService.getIssue(issueBO);
        return BaseRespVo.ok(issue);
    }

    //http://localhost:8083/admin/issue/create
    //PostBody {"question":"123","answer":"123"}
    @PostMapping("create")
    public BaseRespVo create(@RequestBody IssueParamBO issueParamBO) throws AdminVarcharException {

        // 如果issueParamBO 中的长度超过255就会报错
        if (issueParamBO.getQuestion() != null && issueParamBO.getQuestion().length() > 255) {
            throw new AdminVarcharException("您输入问题的字符过长， 已经超过255个字符，请调整后再提交");
        }

        // 如果issueParamBO 中的长度超过255就会报错
        if (issueParamBO.getAnswer() != null && issueParamBO.getAnswer().length() > 255) {
            throw new AdminVarcharException("您输入的回复字符过长, 已经超过255个字符， 请调整后再提交");
        }


        // 创建updateTime 和 addTime
        Date date = new Date();


        Issue issue = new Issue();
        issue.setQuestion(issueParamBO.getQuestion());
        issue.setAnswer(issueParamBO.getAnswer());
        issue.setUpdateTime(date);
        issue.setAddTime(date);
        issue.setDeleted(0);

        BaseIssueBO issueData = issueService.create(issue);

        //createVO = issueService.create(question, answer);
        //{"errno":0,"data":{"id":9,"question":"冰冰","answer":"冰冰","addTime":"2021-07-09 09:27:56","updateTime":"2021-07-09 09:27:56"},"errmsg":"成功"}
        return BaseRespVo.ok(issueData);
    }

    //http://182.92.235.201:8083/admin/issue/delete
    //question
    //{"id":9,"question":"冰冰","answer":"冰冰","addTime":"2021-07-09 09:27:56","updateTime":"2021-07-09 09:27:56"}
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody BaseIssueBO issueBO) {
        Integer affectRow = issueService.delete(issueBO.getId());
        if (affectRow == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除失败");
    }

    //http://localhost:8083/admin/issue/update
    //{"id":8,"question":"是真的吗?","answer":"是真的哦， 全是拼多多正品！修改","addTime":"2021-07-09","updateTime":"2021-07-09","deleted":0}
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Issue issue) throws AdminVarcharException {

        // 如果issue 中的长度超过255就会报错
        if (issue.getQuestion() != null && issue.getQuestion().length() > 255) {
            throw new AdminVarcharException("您输入问题的字符过长， 已经超过255个字符，请调整后再提交");
        }

        // 如果issue 中的长度超过255就会报错
        if (issue.getAnswer() != null && issue.getAnswer().length() > 255) {
            throw new AdminVarcharException("您输入的回复字符过长, 已经超过255个字符， 请调整后再提交");
        }



        Integer affectRow = issueService.update(issue);
        if (affectRow == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("修改失败");
    }
}